<?php
class stock extends goodsPoster {

    private $tab = array( 'inStock' => '入库管理', 'outStock' => '出库管理' );
    private $color = array('深红', '大红','橘红','橘黄 ',
                             '玫瑰红','橙色','深黄','淡黄','草绿',
                            '墨绿', '柠檬黄', '黄绿', '蓝绿', '浅绿',
                           '天蓝', '湖蓝', '黑色', '白色', '紫色',
                           '红灰', '黄灰', '蓝灰', '紫灰', '浅灰');
    private $size = array('080CM','085CM','090CM','095CM','100CM',"105CM","110CM",
                            "115CM","120CM","125CM","130CM","135CM","140CM","145CM","150CM","155CM","160CM");

    public function __construct() {
        parent::__construct();
        $this->pk = "inOutStockNo";
    }

    public function index( $c, $a ) {
        $type = G( 'type', 'inStock' );
        $method = "{$a}_{$type}";
        $this->$method( $c, $a, $type );
    }

    private function index_inStock( $c, $a, $type ) {
        $type = G( 'type', 'inStock' );
        $this->table = 'inoutstock';

        $this->page_size = 2;
        $conditions = array( 'inOutStyle' => '0');
        $result = $this->Paged( G( 'page' ), $this->page_size )->findAll($conditions);

        $pager = $result['page'];
        $result = $result['result'];

        include $this->display( $c );
    }

    private function index_outStock( $c, $a, $type ) {
        $type = G( 'type', 'outStock' );
        $this->table = 'inoutstock';



        $conditions = array( 'inOutStyle' => '1');
        $this->page_size = 2;
        $result = $this->Paged( G( 'page' ), $this->page_size )->findAll($conditions);

        $pager = $result['page'];
        $result = $result['result'];
        include $this->display( $c );
    }

    public function edit( $c, $a ) {
        $type = G( 'type', 'inStock' );
        if ( !$id = intval( G( 'inOutStockNo' ) ) ) {
            $this->error();
            exit;
        }

        $this->table = 'inoutstock'; // 数据库表名
        $result = $this->find( array( 'inOutStockNo' => $id ) ); // 查找数据
        include $this->display( $c ); // 加载模板
    }

    # 删除商品
    public function del( $c ) {
        if ( !$id = intval( G( "inOutStockNo" ) ) ) {
            exit( '非法删除商品' );
        }

        #删除商品
        $this->table = 'inoutstock';
        $msg = $this->delete( array( "inOutStockNo" => $id ) ) ? 'success' : 'failed';

        # 删除附件
        $this->table = 'attachment';
        $this->delete( array( "aid" => $id, 'model' => strtolower( $c ) ) );
        exit( $msg );
    }

    public function search_inStock( $c, $a ) {
        $this->table = 'inoutstock'; // 数据库表名
        $type = G( 'type', 'inStock' );

        $where = array( G( 'k' ) => G( 'v' ) );
        if ( $key = G( 'key' ) ) {
            $tmp = '';
            foreach ( $where as $k => $v ) {
                $tmp .= "{$k}='" . urldecode( $v ) . "' and ";
            }
            $where = $tmp ? ( $tmp . "inOutStockNo ={$key} " ) : '';
        }

        $this->pk = 'inOutStockNo';
        $result = $this->Paged()->findAll( $where );
        $pager = $result[ 'page' ];
        $result = $result[ 'result' ];


        include $this->display( $c ); // 加载模板

    }
    public function search_Goods( $c, $a ) {


        $this->table = 'inOutStockRec'; // 数据库表名
        $type = G( 'type' );

        $where = array( G( 'k' ) => G( 'v' ) );
        if ( $key = G( 'key' ) ) {
            $tmp = '';
            foreach ( $where as $k => $v ) {
                $tmp .= "{$k}='" . urldecode( $v ) . "' and ";
            }
            $where = $tmp ? ( $tmp . "goodsNo ={$key} " ) : '';
        }



        $myType = (($type == 'inStock') ? 0:1);
        if(is_string($where))
            $where = $where . "and inOutStockNo = ".G('inOutStockNo')." and inOutStyle =".$myType;
        else
            $where = " inOutStockNo = ".G('inOutStockNo')." and inOutStyle =".$myType;



        $this->pk = 'inOutStockNo';
        $result = $this->Paged()->findAll( $where );
        $pager = $result[ 'page' ];
        $result = $result[ 'result' ];



        include $this->display( $c ); // 加载模板

    }



    public function search_outStock( $c, $a ) {

        $this->table = 'inoutstock'; // 数据库表名

        $type = G( 'type', 'outStock' );

        $where = array( G( 'k' ) => G( 'v' ) );
        if ( $key = G( 'key' ) ) {
            $tmp = '';
            foreach ( $where as $k => $v ) {
                $tmp .= "{$k}='" . urldecode( $v ) . "' and ";
            }
            $where = $tmp ? ( $tmp . "inOutStockNo ={$key} " ) : '';
        }

        $this->pk = 'inOutStockNo';
        $result = $this->Paged()->findAll( $where );
        $pager = $result[ 'page' ];
        $result = $result[ 'result' ];

        print_r($result);

        include $this->display( $c ); // 加载模板
    }

    public function addOrder( $c, $a,$args ) {
        $type = G('type',$args['type']);
        //print_r($type);
        include $this->display( $c );
    }

    public function addGoods($c,$a,$args)
    {
        $type = G('type');
        $result =array('inOutStockNo'=>G('inOutStockNo'));

        include $this->display($c);
    }

    public function editGoods($c,$a,$args)
    {
        $type = G('type');
        $result =array('inOutStockNo'=>G('inOutStockNo'));
        $inoutsytle = $type == 'inStock' ? 0:1;
        $id = G('id');
        $this->table = 'inOutStockRec';
        $result = $this->find(array('id'=>$id,'inOutStyle'=>$inoutsytle));
        print_r($result);
        include $this->display($c);
    }


    public function lookGoods($c,$a,$args)
    {
        print_r("123");
        $type = G( 'type', 'inStock' );
        $id = G('inOutStockNo');
        $inoutsytle = $type == 'inStock' ? 0:1;


        $this->table = 'inOutStockRec'; // 数据库表名
        $condition = array('inOutStockNo'=>$id, 'inOutStyle'=>$inoutsytle);
        $this->page_size = 5;
        $result = $this->Paged( G( 'page' ), $this->page_size )->findAll($condition, 'id');


        $pager = $result['page'];
        $result = $result['result'];


        //$this->pk = "inOutStockNo";
        include $this->display($c);
    }

    public function saveGoods($c,$a,$args) {

        $myType = G('type');

        if(1 == $_POST['inStockType'])
        {
            /**商品信息表加入**/
            $this->table = 'bsInfo';
            $rows['id'] = $_POST['goodsNo'];
            $rows['materialNo']   = $_POST['clothNo'];
            $rows['supplierName'] = $_POST['supplierName'];
            foreach($_POST['color'] as $key=>$val)
            {
                $rows['color'] = $_POST['color'][$key].','.$rows['color'];
            }
            foreach($_POST['size'] as $key=>$val)
            {
                $rows['size'] = $rows['size'].$_POST['size'][$key];
            }
            $rows['goodsStatus'] = 0;
            $msg = $this->create( $rows ) ? 'success' : 'failed';                         // 添加供货商
        }

        # 获取表单数据
        /**更新商品出入库单表**/
        $this->table = 'inOutStock'; // 数据库表名
        $condition = array('inOutStockNo' => $_POST['inOutStockNo']);
        $result = $this->find($condition);

        /**zouhong-edit**/
        $rows[ 'inOutStockNo' ] = $_POST['inOutStockNo'];


        $this->table = "inOutStockRec";
        $res = $this->find(null, null, "max(id)");
        $maxid = intval($res['max(id)']) + 1;
        $rows['inOutRecList'] = $result['inOutRecList'].$maxid.",";
        /**zouhong-edit-edit**/
        $this->table = 'inOutStock'; // 数据库表名
        $msg = $this->update($condition,$rows) ? 'success' : 'failed';

        unset($rows);

        $rows['goodsNo'] = htmlspecialchars( trim( $_POST['goodsNo'] ) );
        $rows[ 'clothNo' ] = htmlspecialchars( trim( $_POST['clothNo'] ) );
        //**更新出入库记录表、商品库存信息表*
        $this->table = 'inOutStockRec'; // 数据库表名
        $rows['inOutStockNo'] = $_POST['inOutStockNo'];

        if('inStock' == $myType)
        {
            $rows['inOutStyle']= 0 ;
        }else
        {
            $rows['inOutStyle']= 1 ;
        }

        if(count($_POST['color']) > 1)
        {
           $judgeVal = 1;           /***判断同一款衣服有不同颜色**/
        }
        else
        {
            $judgeVal = 0;         /***判断同一款衣服有不同尺码**/
        }

        $rows['goodsNumber'] = $_POST['goodsNumber'];
        if(1 == $judgeVal)
        {
            foreach ( $_POST['color'] as $key => $val ) {
                $this->table = 'inOutStockRec';
                $rows['color'] = $val;
                $rows['size'] =  $_POST['size'][0];

                $msg = $this->create( $rows ) ? 'success' : 'failed';                         // 添加供货商

                $this->table = 'stockInfo';
                //$this->pk = "id";
                $where =  ' goodsNo = '.$rows['goodsNo'].' and color = '.$rows['color'].' and size = '.$rows['size'];
                if ($result = $this->find($where, 'id' )) // 查找数据
                {
                    if('inStock' == $myType)
                    {
                     $sql = "update xbl_stockInfo set goodsNumber = goodsNumber + ".$rows['goodsNumber'].' where '.$where;
                    }
                    else
                    {
                        $sql = "update xbl_stockInfo set goodsNumber = goodsNumber - ".$rows['goodsNumber'].' where '.$where;
                    }
                     $this->exec($sql);
                }
                else
                {
                    $id = $this->create( $rows );
                    $msg = $id ? 'success' : 'failed';
                }
                //$this->pk = "inOutStockNo";
            }
        }
        else
        {
            foreach ( $_POST['size'] as $key => $val ) {
                $this->table = 'inOutStockRec';
                $rows['size'] = $val;
                $rows['color'] = $_POST['color'][0];

                $msg = $this->create( $rows ) ? 'success' : 'failed';                         // 添加供货商

                $this->table = 'stockInfo';
                //$this->pk = "id";
                $where =  ' goodsNo = '.$rows['goodsNo'].' and color = '.$rows['color'].' and size = '.$rows['size'];
                if ($result = $this->find($where, 'id' )) // 查找数据
                {
                    if('inStock' == $myType)
                    {
                        $sql = "update xbl_stockInfo set goodsNumber = goodsNumber + ".$rows['goodsNumber'].' where '.$where;
                    }
                    else
                    {
                        $sql = "update xbl_stockInfo set goodsNumber = goodsNumber - ".$rows['goodsNumber'].' where '.$where;
                    }
                    $this->exec($sql);
                }
                else
                {
                    $id = $this->create( $rows );
                    $msg = $id ? 'success' : 'failed';
                }
                //$this->pk = "inOutStockNo";
            }
        }



        exit( $msg );
    }

    public function save($c,$a) {

        $type = G("type", 'inStock');
        # 获取表单数据

        $rows[ 'inOutStockNo' ] = htmlspecialchars( trim( $_POST['orderNo'] ) );
        $rows[ 'amount' ] = htmlspecialchars( trim( $_POST['totalNum'] ) );
        if($rows['amount'] == null)
             $rows[ 'amount' ] = 0;

        /*****zouhong-edit*/
        $rows['inOutStockDate'] = date("YmdHis");
        $rows['operator'] = $this->admin['author'];
        /*****zouhong-edit-end*/

        $this->table = 'inoutstock';
        # 注意 表单字段必须跟数据库表字段名一样
        if($type == 'inStock')
        {
            $rows['supplierName'] =  htmlspecialchars( trim( $_POST['supplierName'] ) );
            $rows['inOutStyle'] =  0;
        }
        else
        {
            $rows['inOutStyle'] =  1;
        }

        if ( $id = $this->find(  array('inOutStockNo' => $rows[ 'inOutStockNo' ])) ) {
            $msg = $this->update( array( 'inOutStockNo' => $id['inOutStockNo'] ), $rows ) ? 'success' : 'failed';  //更新供货商
        } else {
            $msg = $this->create( $rows ) ? 'success' : 'failed';                         // 添加供货商
        }
        exit( $msg );
    }
}